﻿
$(function () {
    
        //选择省份出现城市
        $("#region").change(function () {
            var $regionId = $("#region").val();
            var url = "/Myaccount/RegionList?parentId=" + $regionId;

            if ($regionId == "") {
                $("#city").empty();
                return false;
            }
            $("#city").empty();
            $("#county").empty();

            var def = $("<option value='0' selected='selected'>城市</option>");
            $("#city").append(def);
            $("#city option").eq(0).attr('selected', 'true');
            //$("#city").selectmenu("refresh");
            
            $("#county").empty();
            var defContry = $("<option value='0' selected='selected'>区县</option>");
            $("#county").append(defContry);
            $("#county option").eq(0).attr('selected', 'true');
            //$("#county").selectmenu("refresh");
           
            KevinShop.Common.Ajax.Get(url, function (data) {
                var cityList = data;

                for (var i = 0; i < cityList.length; i++) {
                    var city = cityList[i];
                    var option = $("<option value=" + city.RegionId + ">" + city.RegionName + "</option>");
                    $("#city").append(option);
                }
            });
        });

        //选择城市出现县城
        $("#city").change(function () {
            var $regionId = $("#city").val();
            var url = "/Myaccount/RegionList?parentId=" + $regionId;

            if ($regionId == "") {
                $("#county").html();
                return;
            }
            $("#county").empty();
            var defContry = $("<option value='0' selected='selected'>区县</option>");
            $("#county").append(defContry);
            $("#county option").eq(0).attr('selected', 'true');
            //$("#county").selectmenu("refresh");
            
            KevinShop.Common.Ajax.Get(url, function (data) {
                var countyList = data;
                
                for (var i = 0; i < countyList.length; i++) {
                    var county = countyList[i];
                    var option = $("<option value=" + county.RegionId + ">" + county.RegionName + "</option>");
                    $("#county").append(option);
                }
            });
        });
        $("#county").change(function () {
            var regionId = $("#county").val();
            $("#RegionId").val(regionId);
        });
    });
    
    var CallBackRegion = function (data) {
        var regionList = data;

        for (var i = 0; i < regionList.length; i++) {
            var region = regionList[i];
            var option = $("<option value=" + region.RegionId + ">" + region.RegionName + "</option>");
            $("#region").append(option);
        }
    };
    var CallBackCity = function (data) {
        var cityList = data;

        for (var i = 0; i < cityList.length; i++) {
            var city = cityList[i];
            var option = $("<option value=" + city.RegionId + ">" + city.RegionName + "</option>");
            $("#city").append(option);
        }
    };

    var CallBackDistinct = function (data) {
        var countyList = data;

        for (var i = 0; i < countyList.length; i++) {
            var county = countyList[i];
            var option = $("<option value=" + county.RegionId + ">" + county.RegionName + "</option>");
            $("#county").append(option);
        }
    };

    function loadRegion(regionId) {
        if (regionId == 0 || regionId === undefined) {
            regionId = 0;
        }
        var url = '/MyAccount/GetRegionById?regionId=' + regionId;
        var url1 = '/MyAccount/RegionList?parentId=';

        KevinShop.Common.Ajax.Get(url, function (data) {
            var ids = data.split(",");
            if (data == "") {
                ids[1] = -1;
                ids[2] = -1;
            }
            //$("#region").empty(); //清空省份SELECT控件 
            //KevinShop.Common.Ajax.Get(url1 + 0, function (data) {
                //CallBackRegion(data);
                $('#region').val(ids[1]);
                $("#city").empty(); //清空城市SELECT控件
                KevinShop.Common.Ajax.Get(url1 + ids[1], function (data1) {
                    CallBackCity(data1);

                    $('#city').val(ids[2]);
                    $("#county").empty();
                    KevinShop.Common.Ajax.Get(url1 + ids[2], function (data2) {
                        CallBackDistinct(data2);
                        $('#county').val(regionId);
                    });
                });
            //});
        });
    }

